package com.example.eventbustest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Button
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.logging.Handler

class MainActivity : AppCompatActivity() {
    companion object {
        private const val TAG = "MY_TAG_MainActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        EventBus.getDefault().register(this)
        MyEventBus.instance.register(this)
        findViewById<Button>(R.id.btn_1).setOnClickListener {
//            EventBus.getDefault().post(MessageEvent("11111"))
            val t = MessageEvent("my test")
            MyEventBus.instance.post(t)
            Thread {
                MyEventBus.instance.post(MessageEvent("thread test"))
                Log.d(TAG, "Thread post thread=${Thread.currentThread()}")
            }.start()
        }
    }

    @MySubscribe(MyThreadMode.CHILD)
    fun myEventTest(event: MessageEvent) {
        Log.d(TAG, "myEventTest event=${event.str} thread=${Thread.currentThread()}")
    }

    @MySubscribe
    fun myTest1(event: MessageEvent) {
        Log.d(TAG, "myTest1 event=${event.str} thread=${Thread.currentThread()}")

    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    fun onMessageEvent() {
////        Log.d(TAG, "event1 event=${event.str} thread=${Thread.currentThread()}")
//    }

/*    @Subscribe(threadMode = ThreadMode.ASYNC)
    fun event2(event: MessageEvent ) {
        Log.d(TAG, "event2 thread=${Thread.currentThread()}")
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    fun eventAsync(event: MessageEvent ) {
        Log.d(TAG, "event3 thread=${Thread.currentThread()}")
    }*/

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        MyEventBus.instance.unregister(this)
    }
}